/*
 * mox_log.c
 *
 *  Created on: Jul 12, 2010
 *      Author: jmonk
 */
#include "mox.h"
#include "mox_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <print.h>

int sprint_log(int *p, chanend log)
{
	char string_holder[200];
	char *string = string_holder;
	int *string_i = (int *)string;
	char *temp;
	char *format;
	unsigned length;
	unsigned i,j;
	unsigned string_index = 0;

	while (((int)(string)-0x10000)&0x03) {
		printstrln("Fixing String\n");
		string++;
	}
	format = (char *)*p;
	p++;
	length = 0;
	while (format[length]) length += 1;

	for (i = 0; i < length; i++) {
		if (format[i] == '%')  {
			if (format[++i] == '%') {
				string[string_index++] = format[i];
			} else {
				switch (format[i]) {
#ifdef INT_SUPPORT
				case 'd':
					sprint_int(string, &string_index, *p, 1);
					p++;
					break;
				case 'u':
					sprint_int(string, &string_index, *p, 0);
					p++;
					break;
#endif
				case 'x':
					sprint_hex(string, &string_index, *p);
					p++;
					break;
				case 'c':
					string[string_index++] = (char)*p;
					break;
				case 's':
					j = 0;
					temp = (char *)*p;
					p++;
					while (temp[j]) string[string_index++] = temp[j++];
					break;
				}
			}
		} else {
			string[string_index++] = format[i];
		}
	}
	string[string_index++] = '\0';

	mox_chan_out_wd(log, LOG_MESSAGE);
	while (string_index & 0x03) string_index++;
	mox_chan_out_wd(log, string_index);
	for (i = 0; i < (string_index/4); i++) {
		mox_chan_out_wd(log, string_i[i]);
	}
	mox_chan_out_wd(log, LOG_END);

	return string_index;
}


void sprint_int(char *string, unsigned *string_index, int val, int signed_fl)
{
	char buffer[30];
	unsigned index = 30;
	unsigned value;
	if (signed_fl && (val < 0)) {
		signed_fl = 1;
		value = -val;
	} else {
		signed_fl = 0;
		value = val;
	}

	while (value) {
		buffer[--index] = (value % 10) + '0';
		value /= 10;
	}

	if (signed_fl) {
		buffer[--index] = '-';
	}

	while (index < 30) {
		string[(*string_index)++] = buffer[index++];
	}

	return;
}

void sprint_hex(char *string, unsigned *string_index, unsigned val)
{
	unsigned i;
	unsigned temp;
	char buffer[8];
	unsigned index = 8;

	for (index = 8; index > 0; index--) {
		temp = val & 0x0f;
		if (temp > 9) {
			buffer[index-1] = temp - 10 + 'A';
		} else {
			buffer[index-1] = temp + '0';
		}
		val >>= 4;
	}

	while (!buffer[index]) index++;		//eliminate leading 0s
	while (index < 30) {
		string[(*string_index)++] = buffer[index++];
	}

	return;
}

int master_log(mox_master_state *state, const char format[], ...)
{
	int i;
	//char buffer[165];
	//__gnuc_va_list ap;

	//va_start(ap,format);
	//n = vsnprintf(buffer,100,format,ap);

//	if (state->log == NULL) {
		printstr(format);
//	} else {
//		sprint_log((int *)&format, state->log->channel);
//	}

	return 0;
}



int slave_log(struct mox_slave_state *state, const char *format, ...)
{
	char buffer[165];
	int n, i;
	//__gnuc_va_list ap;

	//va_start(ap,format);
	//n = vsnprintf(buffer,100,format,ap);

	if (state->log == NULL) {
		printstr(format);
	} else {
		sprint_log((int *)&format, state->log->channel);
	}

	return 0;
}

int module_log(chanend mox, const char *format, ...)
{
	//sprint_log((int *)&format, mox);
	return 0;
}
